package com.gs.common

import android.graphics.Bitmap
import com.gs.common.callback.HttpCallback
import com.gs.common.util.OkHttpUtils

open class API {

    // ----- user ------

    fun login(usernameOrPhoneNum: String, password: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/login"
        OkHttpUtils.doPost(
            url,
            callback,
            "usernameOrPhoneNum" to usernameOrPhoneNum,
            "password" to password
        )
    }

    fun autoLogin(token: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/autoLogin"
        OkHttpUtils.doPost(url, callback, "token" to token)
    }

    fun register(phoneNum: String, username: String, password: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/register"
        OkHttpUtils.doPost(
            url,
            callback,
            "phoneNum" to phoneNum,
            "username" to username,
            "password" to password
        )
    }

    fun logout(username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/logout?username=$username"
        OkHttpUtils.doGet(url, callback)
    }

    fun changePassword(username: String, password: String, changePassword: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/changePassword"
        OkHttpUtils.doPost(url, callback, "username" to username, "password" to password, "changePassword" to changePassword)
    }

    fun getUserInfoByUsername(username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/getDetail?username=$username"
        OkHttpUtils.doGet(url, callback)
    }

    fun getUserInfoByUserId(userId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/getDetailById?userId=$userId"
        OkHttpUtils.doGet(url, callback)
    }

    fun dealAuthority(username: String, isBusiness: Boolean, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/user/dealAuthority"
        OkHttpUtils.doPost(
            url,
            callback,
            "username" to username,
            "isBusiness" to isBusiness.toString()
        )
    }

    fun updateUserInfo(
        username: String, changeNickname: String? = null,
        changeUsername: String? = null, changePhoneNumber: String? = null,
        callback: HttpCallback
    ) {
        val url = "$LOCALHOST_8080_API/user/updateUserInfo"
        OkHttpUtils.doPost(
            url, callback, "username" to username,
            "changeNickname" to changeNickname, "changeUsername" to changeUsername,
            "changePhoneNumber" to changePhoneNumber
        )
    }

    // ----- goods ------

    fun getGoods(callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/goods/getAll"
        OkHttpUtils.doGet(url, callback)
    }

    fun getGoodsById(goodId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/goods/getGoodsById?goodId=$goodId"
        OkHttpUtils.doGet(url, callback)
    }

    fun getGoodsLikeTitleOrLikeDetailTitle(query: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/goods/getGoodsByQuery?query=$query"
        OkHttpUtils.doGet(url, callback)
    }

    fun getGoodsByUsername(username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/goods/getGoods?username=$username"
        OkHttpUtils.doGet(url, callback)
    }

    fun uploadGoodPicture(picture: Bitmap, callback: HttpCallback) {
        val url = "http://${API.IP}:${API.PORT}/file/upload"
        OkHttpUtils.doPostWithPicture(url, picture, callback)
    }

    fun addOrUpdateGood(
        state: String,
        username: String,
        goodId: Int? = null,
        goodTitle: String,
        goodDetailTitle: String,
        goodPicturePath: String,
        goodPrice: Double,
        goodCount: Int,
        callback: HttpCallback
    ) {
        val url = if (state == "添加") {
            "$LOCALHOST_8080_API/goods/addGood"
        } else {
            "$LOCALHOST_8080_API/goods/updateGood"
        }
        OkHttpUtils.doPost(
            url,
            callback,
            "username" to username,
            "goodId" to goodId.toString(),
            "goodTitle" to goodTitle,
            "goodDetailTitle" to goodDetailTitle,
            "goodPicturePath" to goodPicturePath,
            "goodPrice" to goodPrice.toString(),
            "goodCount" to goodCount.toString()
        )
    }

    fun deleteGood(goodId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/goods/delete?goodId=$goodId"
        OkHttpUtils.doGet(url, callback)
    }

    fun getSoldCountByGoodId(goodId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/goods/getSoldCount?goodId=$goodId"
        OkHttpUtils.doGet(url,callback)
    }

    // ----- cart ------

    fun addGoodToCart(goodId: Int, username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/addGood?goodId=$goodId&username=$username"
        OkHttpUtils.doGet(url, callback)
    }

    fun updateCartGoodData(username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/updateCartGoodData?username=$username"
        OkHttpUtils.doGet(url, callback)
    }

    fun addGoodSoldCount(orderId: Int, goodId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/addGoodSoldCount?orderId=$orderId&goodId=$goodId"
        OkHttpUtils.doGet(url, callback)
    }

    fun subtractGoodSoldCount(orderId: Int, goodId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/subtractGoodSoldCount?orderId=$orderId&goodId=$goodId"
        OkHttpUtils.doGet(url, callback)
    }

    fun deleteGoodFromCart(orderId: Int, goodId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/deleteGood?orderId=$orderId&goodId=$goodId"
        OkHttpUtils.doGet(url, callback)
    }

    fun getCartOrderId(username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/getCartOrderId?username=${username}"
        OkHttpUtils.doGet(url, callback)
    }

    fun payOrder(username: String, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/cart/payOrder"
        OkHttpUtils.doPost(url, callback, "username" to username)
    }

    // ----- order ------

    fun getPaidOrdersInfo(username: String, date: String? = null, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/order/getPaidOrders"
        OkHttpUtils.doPost(url, callback, "username" to username, "date" to (date ?: ""))
    }

    fun deleteOrder(orderId: Int, callback: HttpCallback) {
        val url = "$LOCALHOST_8080_API/order/deleteOrder?orderId=$orderId"
        OkHttpUtils.doGet(url, callback)
    }

    companion object {
//        const val IP = "litchi-source.superlwb0608.top"
        const val IP = "192.168.10.38"
        const val PORT = "8080"
        private const val LOCALHOST_8080_API = "http://$IP:$PORT/api"

        private var instance: API? = null
        fun getInstance(): API {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = API()
                    }
                }
            }
            return instance!!
        }
    }

}